/**
 * 作者: hxl
 * 日期: 2025-05-10 17:31:58
 * 备注: 联盟限时冲榜表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUnionActTopRank;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UnionActTopRankModel extends BaseModel implements Model {
    public static final String LISTKEY = "union_id.act_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // id
    protected long unionId; // 联盟id
    protected long actId; // 活动id
    protected int actRankType; // 活动榜单类型
    protected double score; // 分数
    protected long actTime; // 活动开始时间
    protected int rankGet; // 是否领取奖励 (1:已领取；0:未领取)
    protected double initScore; // 初始分数
    protected double maxScore; // 最高分

    public void reset() {
    }

    protected String getKey() {
        return "UnionActTopRank." + unionId + "." + actId;
    }

    protected String getListKey() {
        return "UnionActTopRank." + unionId;
    }

    protected String getListItemKey() {
        return actId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("union_act_top_rank"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("unionId", unionId); // 联盟id
        obj.put("actId", actId); // 活动id
        obj.put("actRankType", actRankType); // 活动榜单类型
        obj.put("score", score); // 分数
        obj.put("actTime", actTime); // 活动开始时间
        obj.put("rankGet", rankGet); // 是否领取奖励 (1:已领取；0:未领取)
        obj.put("initScore", initScore); // 初始分数
        obj.put("maxScore", maxScore); // 最高分
        return obj;
    }

    public PBUnionActTopRank.Builder takeInitBuilder() {
        PBUnionActTopRank.Builder sb = PBUnionActTopRank.newBuilder();
        if (actRankType != 0) {
            sb.setActRankType(actRankType);
        }
        if (score != 0) {
            sb.setScore(score);
        }
        if (maxScore != 0) {
            sb.setMaxScore(maxScore);
        }
        return sb;
    }

    public PBUnionActTopRank.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUnionActTopRank.Builder sb = PBUnionActTopRank.newBuilder();
        if (isFieldUpdate("actRankType")) {
            sb.setActRankType(actRankType);
        }
        if (isFieldUpdate("score")) {
            sb.setScore(score);
        }
        if (isFieldUpdate("maxScore")) {
            sb.setMaxScore(maxScore);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUnionId() {
        return unionId;
    }

    public void putUnionId(long unionId) {
        if (this.unionId != unionId) {
            this.unionId = unionId;
            updateRedisHashField(getKey(), "unionId", this.unionId);
        }
    }

    public void addUnionId(long value) {
        this.unionId += value;
        updateRedisHashField(getKey(), "unionId", this.unionId);
    }

    public long getActId() {
        return actId;
    }

    public void putActId(long actId) {
        if (this.actId != actId) {
            this.actId = actId;
            updateRedisHashField(getKey(), "actId", this.actId);
        }
    }

    public void addActId(long value) {
        this.actId += value;
        updateRedisHashField(getKey(), "actId", this.actId);
    }

    public int getActRankType() {
        return actRankType;
    }

    public void putActRankType(int actRankType) {
        if (this.actRankType != actRankType) {
            this.actRankType = actRankType;
            updateRedisHashField(getKey(), "actRankType", this.actRankType);
        }
    }

    public void addActRankType(int value) {
        this.actRankType += value;
        updateRedisHashField(getKey(), "actRankType", this.actRankType);
    }

    public double getScore() {
        return score;
    }

    public void putScore(double score) {
        if (this.score != score) {
            this.score = score;
            updateRedisHashField(getKey(), "score", this.score);
        }
    }

    public void addScore(double value) {
        this.score += value;
        updateRedisHashField(getKey(), "score", this.score);
    }

    public long getActTime() {
        return actTime;
    }

    public void putActTime(long actTime) {
        if (this.actTime != actTime) {
            this.actTime = actTime;
            updateRedisHashField(getKey(), "actTime", this.actTime);
        }
    }

    public void addActTime(long value) {
        this.actTime += value;
        updateRedisHashField(getKey(), "actTime", this.actTime);
    }

    public int getRankGet() {
        return rankGet;
    }

    public void putRankGet(int rankGet) {
        if (this.rankGet != rankGet) {
            this.rankGet = rankGet;
            updateRedisHashField(getKey(), "rankGet", this.rankGet);
        }
    }

    public void addRankGet(int value) {
        this.rankGet += value;
        updateRedisHashField(getKey(), "rankGet", this.rankGet);
    }

    public double getInitScore() {
        return initScore;
    }

    public void putInitScore(double initScore) {
        if (this.initScore != initScore) {
            this.initScore = initScore;
            updateRedisHashField(getKey(), "initScore", this.initScore);
        }
    }

    public void addInitScore(double value) {
        this.initScore += value;
        updateRedisHashField(getKey(), "initScore", this.initScore);
    }

    public double getMaxScore() {
        return maxScore;
    }

    public void putMaxScore(double maxScore) {
        if (this.maxScore != maxScore) {
            this.maxScore = maxScore;
            updateRedisHashField(getKey(), "maxScore", this.maxScore);
        }
    }

    public void addMaxScore(double value) {
        this.maxScore += value;
        updateRedisHashField(getKey(), "maxScore", this.maxScore);
    }

}